#!/bin/sh

[ "${ACTION}" = "released" ] || exit 0

qssgreen="/sys/devices/platform/leds-gpio/leds/tp-link:green:qss"

umount_disk(){
	/etc/init.d/aria2 stop
	/etc/init.d/samba stop
	/sbin/block umount
}

wifi_switch(){
	[ `uci get wireless.radio0.disabled` -eq 1 ] && uci del wireless.radio0.disabled && uci commit wireless
	state="`uci get wireless.@wifi-iface[0].disabled`"
	case $state in
	1)
		uci del wireless.@wifi-iface[0].disabled && uci commit wireless && /sbin/wifi &> /dev/null;;
	*)
		uci set wireless.@wifi-iface[0].disabled=1 && uci commit wireless && /sbin/wifi &> /dev/null;;
	esac
}

aria2_switch(){
	[ -z `which aria2ctl` ] && exit 1
	pgrep aria2c >/dev/null 2>&1 || exit 1

	aria2AT="`/usr/sbin/aria2ctl -status | grep 'ActiveTask' | awk -F ':' '{print $2}'`"

	if [ $aria2AT -gt 0 ];then
	/usr/sbin/aria2ctl -pause &> /dev/null
	else
	/usr/sbin/aria2ctl -resume &> /dev/null
	fi
}

[ "$SEEN" -lt 1 ] && wifi_switch
[ "$SEEN" -gt 1 -a "$SEEN" -lt 4 ] && aria2_switch
[ "$SEEN" -ge 4 ] && {
	if [ -n "`mount | grep /dev/sd`" ];then
	umount_disk &> /dev/null
	echo 1 > ${qssgreen}/brightness
	echo heartbeat > ${qssgreen}/trigger
	fi
}
